package zmee.pur.src.formplugin.filter;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.scm.pds.common.extfilter.ExtFilterContext;
import kd.scm.pds.common.extfilter.IExtFilterPlugin;
import kd.scm.pds.common.util.PdsCommonUtils;
import kd.scm.pds.common.util.PdsParameterUtils;

import java.util.*;

/**
 * @Author: xw
 * @Date: 2022/11/16/10:36
 * @Description: 自动议价--议价标的过滤--按议价规则(前几名供应商)
 * 竞争性谈判自动议价方案（总金额）
 */
public class NegotiateFilterByRule implements IExtFilterPlugin<ExtFilterContext> {
    private static final long serialVersionUID = 1L;

    public NegotiateFilterByRule() {
    }

    @Override
    public Map<String, Object> getQFilter(ExtFilterContext context) {
        return this.getQFilterMap(this.getQFilterByNegRule(context), (StringBuilder) null);
    }

    private QFilter getQFilterByNegRule(ExtFilterContext context) {
        long projectId = PdsCommonUtils.object2Long(context.getParamMap().get("projectid"));
        QFilter compareFilter = new QFilter("id", "=", projectId);
        DynamicObject compare = QueryServiceHelper.queryOne("src_compare", "id,billno", compareFilter.toArray());
        if (null == compare) {
            return null;
        } else {
            String billNo = compare.getString("billno");
            QFilter filter = new QFilter("project.billno", "=", billNo);
            int size = QueryServiceHelper.query("src_negotiatebill", "id", filter.toArray()).size();
            if (size == 0) {
                //首轮
                DynamicObject compareConfig = QueryServiceHelper.queryOne("src_compare_config", "negotiaterule,topsupplier", compareFilter.toArray());
                if (null == compareConfig) {
                    return null;
                } else {
                    String negotiateRule = PdsCommonUtils.object2String(compareConfig.get("negotiaterule"), "3");
                    int topSupplier = compareConfig.getInt("topsupplier");
                    Set<String> resultSet = PdsParameterUtils.getResultValueSet();
                    QFilter qfilter = null;
                    byte var11 = -1;
                    switch (negotiateRule.hashCode()) {
                        case 49:
                            if (negotiateRule.equals("1")) {
                                var11 = 0;
                            }
                            break;
                        case 50:
                            if (negotiateRule.equals("2")) {
                                var11 = 1;
                            }
                            break;
                        default:
                            throw new IllegalStateException("Unexpected value: " + negotiateRule.hashCode());
                    }

                    switch (var11) {
                        case 0:
                            qfilter = new QFilter("sysresult", "in", resultSet);
                            break;
                        case 1:
                            qfilter = new QFilter("rank", "<=", topSupplier);
                            break;
                        default:
                            throw new IllegalStateException("Unexpected value: " + var11);
                    }

                    return qfilter;
                }
            } else {
                Set<Long> ids = FilterByRule.getIdsByPrice(filter, compareFilter, projectId);
                return new QFilter("id", "in", ids);
            }
        }
    }
}
